package com.liu.lab03.Servlet;

import com.liu.lab03.dao.MusicDao;
import com.liu.lab03.pojo.Music;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "updateMusicServlet", value = "/updateMusic")
public class UpdateMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //            收集音乐信息
        int id = Integer.parseInt(request.getParameter("id"));
        String musicname = request.getParameter("musicname");
        String singer = request.getParameter("singer");
        int ranking = Integer.parseInt(request.getParameter("ranking"));


        // 使用我们当且的自定义类去打包数据
        Music music = new Music();
        music.setId(id);
        music.setMusicname(musicname);
        music.setSinger(singer);
        music.setRanking(ranking);
        
        // 刘小文  -2420710103---
        //调用我们 servlet 的方法访问数据库
        MusicDao dao = new MusicDao();
        int rows = dao.updateMusic(music);


        // 根据数据库的返回值 显示选择结果
        if(rows>0){
            response.sendRedirect("showAllMusics");
        }else{
            PrintWriter out = response.getWriter();
            out.println("修改失败！ <a href='showAllMusics'>返回</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
